package com.ck.ckcloud.powerenvironment.domain.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ck.ckcloud.powerenvironment.core.enums.DeviceTypeEnum;
import com.ck.ckcloud.powerenvironment.core.enums.ManageStatusEnum;
import com.ck.ckcloud.powerenvironment.domain.entity.Device;
import com.ck.ckcloud.powerenvironment.domain.entity.Room;
import com.ck.ckcloud.powerenvironment.domain.entity.Site;
import com.ck.ckcloud.powerenvironment.infrastructure.persistence.DO.DeviceDO;
import com.ck.ckcloud.powerenvironment.infrastructure.persistence.DO.RoomDO;
import com.ck.ckcloud.powerenvironment.infrastructure.persistence.DO.SiteDO;
import com.ck.ckcloud.powerenvironment.infrastructure.repository.DeviceRepository;
import com.ck.ckcloud.powerenvironment.infrastructure.repository.RoomsRepository;
import com.ck.ckcloud.powerenvironment.infrastructure.repository.SitesRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 动环领域服务
 *
 * @author Aiden <a href="mailto:adam602260@gmail.com"/>
 * @version NetworkManagementService.java v0.1
 * @create 2019-09-20 19:11
 */
@Service
public class PowerEnvironmentService {

	@Autowired
	private SitesRepository sitesRepository;

	@Autowired
	private RoomsRepository roomsRepository;

	@Autowired
	private DeviceRepository deviceRepository;

	/**
	 * 分页查询资源
	 * @param page
	 * @return
	 */
	public IPage listSitesByPage(Page page){
		IPage iPage = sitesRepository.listByPage(page);
		List<Site> siteList = new ArrayList<>();

		iPage.getRecords().stream().forEach(s -> {
			SiteDO siteDO = (SiteDO) s;
			Site site = new Site();
			BeanUtils.copyProperties(siteDO, site);
			siteList.add(site);
		});
		iPage.setRecords(siteList);
		return iPage;
	}

	/**
	 * 分页查询资源
	 * @param page
	 * @return
	 */
	public IPage listRoomsByPage(Page page){
		IPage iPage = roomsRepository.listByPage(page);
		List<Room> roomList = new ArrayList<>();

		iPage.getRecords().stream().forEach(s -> {
			RoomDO roomDO = (RoomDO) s;
			Room room = new Room();
			BeanUtils.copyProperties(roomDO, room);
			roomList.add(room);
		});
		iPage.setRecords(roomList);
		return iPage;
	}

	/**
	 * 分页查询设备
	 * @param page
	 * @return
	 */
	public IPage listDevicesByPage(Page page){
		IPage iPage = deviceRepository.listByPage(page);
		List<Device> deviceList = new ArrayList<>();

		iPage.getRecords().stream().forEach(s -> {
			DeviceDO deviceDO = (DeviceDO) s;
			Device device = new Device();
			BeanUtils.copyProperties(deviceDO, device);
			device.setManageStatus(ManageStatusEnum.getManageStatus(deviceDO.getManageStatus()));
			device.setRoomName(
				roomsRepository
					.getByRoomId(deviceDO.getRoomId())
					.getRoomname()
			);
			device.setDeviceType(DeviceTypeEnum.getType(deviceDO.getDeviceTypeId()));
			deviceList.add(device);
		});
		iPage.setRecords(deviceList);
		return iPage;
	}

}
